package com.sfc.spiderweb.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sfc.spiderweb.entity.CurrentOrder;
import com.sfc.spiderweb.entity.OrderGenTime;
import com.sfc.spiderweb.entity.OrderModel;
import com.sfc.spiderweb.mapper.CurrentOrderMapper;
import com.sfc.spiderweb.mapper.OrderGenTimeMapper;
import com.sfc.spiderweb.mapper.OrderModelMapper;
import com.sfc.spiderweb.service.LazadaActivityService;
import com.sfc.spiderweb.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 李振伟(Earl Li)
 * @title: LazadaActivityServiceImpl
 * @projectName spider-web
 * @description: ${description}
 * @date 2019/12/1916:34
 */
@Slf4j
@Service
public class LazadaActivityServiceImpl implements LazadaActivityService {

    @Resource
    private OrderModelMapper orderModelMapper;

    @Resource
    private OrderGenTimeMapper orderGenTimeMapper;

    @Resource
    private CurrentOrderMapper currentOrderMapper;

    private Date nowDate;

    @Override
    public void lazadaActivityTask(String SpiderHandlers) {
        String netDateStr = DateUtil.getNetTime();
        log.info("当前时间：" + netDateStr);
        this.nowDate = DateUtil.parse(netDateStr, "yyyy-MM-dd HH:mm:ss");
        String platform;
        String orderType;
        String orderGroup;
        String orderName;
        String dateStr;
        boolean amazonDay = false;
        switch (SpiderHandlers) {
            case "AMAZON_MONTH":
                platform = "amazon";
                orderType = "spider_amazon";
                orderGroup = "amazon_month";
                orderName = "AMAZON_MONTH";
                dateStr = DateUtil.formatDate("yyyy-MM", nowDate);
                break;
            case "AMAZON_FLOW":
                platform = "amazon";
                orderType = "spider_amazon";
                orderGroup = "amazon_flow";
                orderName = "AMAZON_FLOW";
                dateStr = DateUtil.formatDate("yyyy-MM-dd", nowDate);
                break;
            case "AMAZON_DAY":
                platform = "amazon";
                orderType = "spider_amazon";
                orderGroup = "amazon_day";
                orderName = "AMAZON_DAY";
                amazonDay = true;
                dateStr = DateUtil.formatDate("yyyy-MM-dd", nowDate);
                break;
            case "AMAZON_SCREENSHOT":
                platform = "amazon";
                orderType = "spider_amazon";
                orderGroup = "amazon_screenshot";
                orderName = "AMAZON_SCREENSHOT";
                dateStr = DateUtil.formatDate("yyyy-MM-dd", nowDate);
                break;
            case "LAZADA_MONTH":
                platform = "lazada";
                orderType = "spider_lazada";
                orderGroup = "lazada_month";
                orderName = "LAZADA_MONTH";
                dateStr = DateUtil.formatDate("yyyy-MM", nowDate);
                break;
            case "LAZADA_PERFORMANCE":
                platform = "lazada";
                orderType = "spider_lazada";
                orderGroup = "lazada_performance";
                orderName = "LAZADA_PERFORMANCE";
                dateStr = DateUtil.formatDate("yyyy-MM-dd", nowDate);
                break;
            case "LAZADA_ACTIVITY":
                platform = "lazada";
                orderType = "spider_lazada";
                orderGroup = "lazada_activity";
                orderName = "LAZADA_ACTIVITY";
                dateStr = DateUtil.formatDate("yyyy-MM-dd", nowDate);
                break;
            case "LAZADA_ACTIVITY_ID":
                platform = "lazada";
                orderType = "spider_lazada";
                orderGroup = "lazada_activity_id";
                orderName = "LAZADA_ACTIVITY_ID";
                dateStr = DateUtil.formatDate("yyyy-MM-dd", nowDate);
                break;
            case "AMAZON_REPORTSANDREVIEWS":
                platform = "amazon";
                orderType = "amazon";
                orderGroup = "amazon_reportsandrev";
                orderName = "AMAZON_REPORTSANDREVIEWS";
                dateStr = DateUtil.formatDate("yyyy-MM-dd", nowDate);
                break;
            case "AMAZON_NOTSALESLISTING":
                platform = "amazon";
                orderType = "amazon";
                orderGroup = "amazon_notsaleslisti";
                orderName = "AMAZON_NOTSALESLISTING";
                dateStr = DateUtil.formatDate("yyyy-MM-dd", nowDate);
                break;
            default:
                log.info("参数不合法");
                throw new RuntimeException("参数不合法");
        }
        //删除之前生成的命令
        QueryWrapper<CurrentOrder> currentOrderQueryWrapper = new QueryWrapper<>();
        currentOrderQueryWrapper.lambda().eq(CurrentOrder::getPlatform, platform);
        currentOrderQueryWrapper.lambda().eq(CurrentOrder::getOrderGroup, orderGroup);
        currentOrderMapper.delete(currentOrderQueryWrapper);
        log.info("删除之前作废的命令");
        //更改账单下载配置时间
        QueryWrapper<OrderModel> orderModelQueryWrapper = new QueryWrapper<>();
        orderModelQueryWrapper.lambda().eq(OrderModel::getOrderType, orderType);
        orderModelQueryWrapper.lambda().eq(OrderModel::getOrderGroup, orderGroup);
        orderModelQueryWrapper.lambda().eq(OrderModel::getPlatform, platform);
        orderModelQueryWrapper.lambda().eq(OrderModel::getOrderName, orderName);
        OrderModel orderModel = orderModelMapper.selectOne(orderModelQueryWrapper);
        if (!StringUtils.isEmpty(orderModel)) {
            Map<String, String> map = new HashMap<>();
            map.put("nowDate", dateStr);
            orderModel.setOrderValue(JSONObject.toJSONString(map));
            orderModel.setUpdateTime(nowDate);
            int i = orderModelMapper.updateById(orderModel);
            if (i > 0) {
                log.info("更新命令时间配置成功!");
            } else {
                log.info("更新命令时间配置失败!");
            }
        }
        //更改生成命令设置
        QueryWrapper<OrderGenTime> orderGenTimeQueryWrapper = new QueryWrapper<>();
        orderGenTimeQueryWrapper.lambda().eq(OrderGenTime::getOrderType, orderType);
        orderGenTimeQueryWrapper.lambda().eq(OrderGenTime::getOrderGroup, orderGroup);
        orderGenTimeQueryWrapper.lambda().eq(OrderGenTime::getPlatform, platform);
        OrderGenTime orderGenTime = orderGenTimeMapper.selectOne(orderGenTimeQueryWrapper);
        if (!StringUtils.isEmpty(orderGenTime)) {
            if (amazonDay) {
                log.info("amazon日转款命令amazonDay=" + amazonDay);
                Date date = amazonTransferChooseDate(nowDate);
                log.info("下发命令时间" + DateUtil.format(date, "yyyy-MM-dd HH:mm:ss"));
                if (!StringUtils.isEmpty(date)) {
                    orderGenTime.setOrderGenTime(date);
                    orderGenTime.setOrderStatus(1);
                }
            } else {
                orderGenTime.setOrderGenTime(nowDate);
                orderGenTime.setOrderStatus(1);
            }
            orderGenTime.setUpdateTime(nowDate);
            int i = orderGenTimeMapper.updateById(orderGenTime);
            if (i > 0) {
                log.info("命令改为待生成状态成功!");
            } else {
                log.info("命令改为待生成状态失败!");
            }
        }
    }

    private static Date amazonTransferChooseDate(Date nowDate) {
        String thisWeek = DateUtil.getThisWeek(nowDate);
        log.info("thisWeek=" + thisWeek);
        switch (thisWeek) {
            case "星期一":
                String dateStr1 = DateUtil.formatDate("yyyy-MM-dd 00:10:00", nowDate);
                return DateUtil.parse(dateStr1, "yyyy-MM-dd HH:mm:ss");
            case "星期二":
                String dateStr2 = DateUtil.formatDate("yyyy-MM-dd 00:40:00", nowDate);
                return DateUtil.parse(dateStr2, "yyyy-MM-dd HH:mm:ss");
            case "星期三":
                String dateStr3 = DateUtil.formatDate("yyyy-MM-dd 01:10:00", nowDate);
                return DateUtil.parse(dateStr3, "yyyy-MM-dd HH:mm:ss");
            case "星期四":
                String dateStr4 = DateUtil.formatDate("yyyy-MM-dd 01:40:00", nowDate);
                return DateUtil.parse(dateStr4, "yyyy-MM-dd HH:mm:ss");
            case "星期五":
                String dateStr5 = DateUtil.formatDate("yyyy-MM-dd 02:10:00", nowDate);
                return DateUtil.parse(dateStr5, "yyyy-MM-dd HH:mm:ss");
            case "星期六":
                String dateStr6 = DateUtil.formatDate("yyyy-MM-dd 02:40:00", nowDate);
                return DateUtil.parse(dateStr6, "yyyy-MM-dd HH:mm:ss");
        }
        return null;
    }

}
